package com.aibili.common;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {

	protected static final Logger LOG = LoggerFactory.getLogger(JPushUtil.class);
	private final String APPKEY = "54cd070c2651f24bbd411ddc";
	private final String MASTERSECRET = "7d3f01e03e39b4eea94b828e";
	private JPushClient jPushClient;

	public JPushUtil() {
		jPushClient = new JPushClient(MASTERSECRET, APPKEY);
	}

	public void testsingleNotify(String regid) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios())
				.setAudience(Audience.registrationId(regid))
				.setOptions(Options.newBuilder().setApnsProduction(true).build())
				.setNotification(Notification.newBuilder().setAlert("alert content")
						.addPlatformNotification(AndroidNotification.newBuilder().setTitle("Android Title").build())
						.addPlatformNotification(IosNotification.newBuilder().incrBadge(1)
								.setSound("notification_sounds.mp3").addExtra("messagetype", "0").build())
						.build())
				.build();
		try {
			jPushClient.sendPush(payload);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Description 发送给单个android用户
	 * @param reg_id
	 * @param message
	 */
	public void sendSingleAndroidPush(String reg_id, String message) {
		// message = "{\"title\":\"" + "爱杭州" +
		// "\",\"description\":\"gsdfgdfgsdfgsdfg\"," +
		// "\"custom_content\":{\"messagetype\":\"0\",\"id\":\"0\"}}";
		try {
			PushResult result = jPushClient.sendAndroidMessageWithRegistrationID("友着", message, reg_id);
			LOG.info("Got result - " + result);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Description 发送给单个ios用户
	 * @param reg_id
	 * @param message
	 * @param extras
	 */
	public void sendSingleIOSPush(String reg_id, String message, HashMap<String, String> extras) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios())
				.setAudience(Audience.registrationId(reg_id))
				.setOptions(Options.newBuilder().setApnsProduction(false).build())
				.setNotification(
						Notification.newBuilder()
								.addPlatformNotification(IosNotification.newBuilder().setAlert(message).setBadge(1)
										.setSound("notification_sounds.mp3").addExtras(extras).build())
								.build())
				.build();
		try {
			PushResult result = jPushClient.sendPush(payload);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Description 发送给所有的用户
	 * @param title
	 * @param message
	 */
	public void sendAllNotificationPush(String title, String message) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
				.setOptions(Options.newBuilder().setApnsProduction(true).build())
				.setNotification(
						Notification.newBuilder().setAlert(message)
								.addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).build())
								.addPlatformNotification(IosNotification.newBuilder().incrBadge(1)
										.setSound("notification_sounds.mp3").setAlert(message).build())
								.build())
				.build();
		try {
			jPushClient.sendPush(payload);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Description 发送给所有的android用户
	 * @param message
	 * @param extras
	 */
	public void sendAllAndroidPush(String message, HashMap<String, String> extras) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
				.setNotification(Notification.android(message, "友着", extras)).build();
		try {
			jPushClient.sendPush(payload);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Description 发送给所有的ios用户
	 * @param message
	 * @param extras
	 */
	public void sendAlliosPush(String message, HashMap<String, String> extras) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder().setAlert(message).setBadge(5)
								.setSound("notification_sounds.mp3").addExtras(extras).build())
						.build())
				.setMessage(Message.content(message)).setOptions(Options.newBuilder().setApnsProduction(false).build())
				.build();

		// PushPayload payload =
		// PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
		// .setOptions(Options.newBuilder().setApnsProduction(true).build())
		// .setNotification(
		// Notification.newBuilder()
		// .addPlatformNotification(IosNotification.newBuilder().setAlert(message).setBadge(1)
		// .setSound("notification_sounds.mp3").addExtras(extras).build())
		// .build())
		// .build();
		try {
			PushResult result = jPushClient.sendPush(payload);
			LOG.info("Got result - " + result);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Description 给所有的用户发送
	 * @param content
	 * @param extras
	 */
	public void sendAll(String content, HashMap<String, String> extras) {
		// sendAlliosPush(content, extras);
		// sendAllAndroidPush(content, extras);
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
				.setOptions(Options.newBuilder().setApnsProduction(false).build())
				.setNotification(
						Notification.newBuilder().setAlert(content)
								.addPlatformNotification(AndroidNotification.newBuilder().setTitle(content).build())
								.addPlatformNotification(
										IosNotification.newBuilder().incrBadge(1).setAlert(content).build())
								.build())
				.setMessage(Message.content(content)).build();
		try {
			PushResult result = jPushClient.sendPush(payload);
			LOG.info("Got result - " + result);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

}
